package me.android.library.ui.impl;

import android.content.res.Resources;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import me.android.library.common.service.AbstractService;
import me.android.library.ui.UiService;
import me.android.library.ui.api.LayoutLoader;
import me.android.library.ui.api.Page;
import me.android.library.ui.config.PageInfo;


@Slf4j
public abstract class AbstractLoader extends AbstractService implements LayoutLoader {

    protected FragmentActivity main;
    protected FragmentManager fragmentManager;
    protected Resources resources;
    protected String pageKey;

    @Getter
    @Setter
    protected String key;

    @Override
    public void layout(FragmentActivity main) {
        this.main = main;
        this.fragmentManager = main.getSupportFragmentManager();
        this.resources = main.getResources();
    }

    synchronized protected Page addFragment(int containerResId, String pageKey, Bundle args) {

        Preconditions.checkNotNull(pageKey);
        Page page = UiService.getInstance().createPage(pageKey);
        Fragment frm = (Fragment) page;

        if (fragmentManager.findFragmentByTag(pageKey) != null) {
            log.debug("page exist: {}", pageKey);
            return page;
        }

        if (args != null) {
            frm.setArguments(args);
        }

        FragmentTransaction ft = fragmentManager.beginTransaction();
        setPageAnim(pageKey, ft);

        boolean isAdded = frm.isAdded();
        if(!isAdded){
            ft.add(containerResId, frm, pageKey)
                    .addToBackStack(pageKey)
                    .commitAllowingStateLoss();

            log.debug("page add: {}", pageKey);
        }
        else {
            ft.show(frm);
        }

//        if (isAdded) {
//            ft.detach(frm)
//                    .add(containerResId, frm, pageKey)
//                    .addToBackStack(pageKey)
//                    .attach(frm)
//                    .commitAllowingStateLoss();
//
//            log.debug("page added: {}", pageKey);
//        } else {
//            ft.add(containerResId, frm, pageKey)
//                    .addToBackStack(pageKey)
//                    .commitAllowingStateLoss();
//
//            log.debug("page add: {}", pageKey);
//        }

        return page;
    }

    @Override
    public void onPageInActivated(String pageKey) {

    }

    @Override
    public void onPageActivated(String pageKey) {
        this.pageKey = pageKey;
    }

    @NonNull
    @Override
    public String toString() {
        if (!Strings.isNullOrEmpty(key)) {
            return key;
        } else {
            return getClass().getName();
        }
    }

    void setPageAnim(String pageKey, FragmentTransaction ft) {
        PageInfo pi = UiService.getInstance().getPageInfo(pageKey);
        int pageIn = pi.getAnimInResId();
        int pageOut = pi.getAnimOutResId();

        int animIn = pageIn > 0 ? pageIn : android.R.anim.fade_in;
        int animOut = pageOut > 0 ? pageOut : android.R.anim.fade_out;

        ft.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.setCustomAnimations(animIn, animOut);
    }

}
